<template>
  <div>
    <PageWrapper :contentFullHeight="true" :contentBackground="true">
      <BasicTable @register="registerTable">
        <template #toolbar>
          <a-button v-auth="['system:part:doc:create']" type="primary" @click="handleCreate">
            {{ t('common.create') }}
          </a-button>
          <a-button
            v-auth="['system:part:doc:storage']"
            type="dashed"
            @click="handleInboundSetting"
          >
            {{ t('routes.demo.PartAndDrawingSettings.inboundSetting') }}
          </a-button>
        </template>
        <template #action="{ record }">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
                auth: 'system:part:doc:update',
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: t('common.confirmDelete'),
                  confirm: handleDelete.bind(null, record),
                },
                auth: 'system:part:doc:delete',
              },
            ]"
          />
        </template>
      </BasicTable>
      <!-- 零件和图文档关联关系弹窗 -->
      <PartAndDrawModal @register="registerPartAndDrawModal" @success="reload" />
      <!-- 入库设置弹窗 -->
      <InboundSettingModal @register="registerInboundSettingModal" />
    </PageWrapper>
  </div>
</template>

<script setup lang="ts">
  import { useI18n } from '/@/hooks/web/useI18n';
  import { PageWrapper } from '/@/components/Page';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';

  import { getPartAndDrawingColumns } from './data';
  import PartAndDrawModal from './PartAndDrawModal.vue';
  import InboundSettingModal from './InboundSettingModal.vue';

  import { getPartDocTypeListAndInStore, deletePartDocTypeList } from '/@/api/demo/system';

  const { t } = useI18n();
  const { createMessage } = useMessage();

  // 新增
  const handleCreate = () => {
    openPartAndDrawModal(true, { isUpdate: false });
  };

  // 修改
  const handleEdit = (record: Recordable) => {
    openPartAndDrawModal(true, { isUpdate: true, record });
  };

  // 删除
  const handleDelete = (record: Recordable) => {
    const newObj = Object.assign({}, record);
    delete newObj.key;
    deletePartDocTypeList(newObj).then((res) => {
      if (res) {
        createMessage.success(t('common.saveSuccess'));
        reload();
      }
    });
  };

  // 入库设置
  const handleInboundSetting = () => {
    getPartDocTypeListAndInStore().then((res) => {
      openInboundSettingModal(true, res);
    });
  };

  // 初始化表格
  const [registerTable, { reload }] = useTable({
    titleHelpMessage: 'PartAndDrawingsIndex',
    api: getPartDocTypeListAndInStore,
    fetchSetting: {
      listField: 'partDocList',
    },
    columns: getPartAndDrawingColumns(),
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    canResize: false,
    actionColumn: {
      width: 80,
      title: t('common.action'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
  });

  // 零件和图文档关联关系弹窗
  const [registerPartAndDrawModal, { openModal: openPartAndDrawModal }] = useModal();

  // 入库设置弹窗
  const [registerInboundSettingModal, { openModal: openInboundSettingModal }] = useModal();
</script>
